<?php
ob_start();
$lb['current_path'] = '..';
$lb['lang']			= 'en';

if($lb['lang'] != 'en'){
	include_once('./language/en.inc.php');
}

include_once($lb['current_path'] . '/include/Smarty/libs/Smarty.class.php');

class lb_smarty extends Smarty {
	
	function __construct(){
		global $lb;
		
		@define('SMARTY_DIR', $lb['current_path'] . '/include/Smarty/libs/');
		$this->template_dir = './templates/default';
		$this->compile_dir =  $lb['current_path'] . '/templates_c/';
		
		$this->security_settings['ALLOW_CONSTANTS'] = true;
	}
}

$lb['smarty'] 	= new lb_smarty();
$lb['smarty']->assign ('css', './templates/default/main.css');

if(isset($_GET['install']) && $_GET['install'] == 'system'){
	$config = "<?php

// database information
\$lb['db_type'] 		= '{$_POST['db_type']}';
\$lb['db_host'] 		= '{$_POST['db_host']}';
\$lb['db_name'] 		= '{$_POST['db_name']}';
\$lb['db_user'] 		= '{$_POST['db_user']}';
\$lb['db_pass'] 		= '{$_POST['db_pass']}';
\$lb['prefix']		= '{$_POST['perfix']}';

// root directory *** with forward slash ***
\$lb['dir']     		= '{$_POST['dir']}';

// link of web directory *** with forward slash ***
\$lb['webdir']  		= '{$_POST['webdir']}';

//Cookie name
\$lb['cookie']		= '{$_POST['cookie']}';
//Cookie information
//\$lb['cookie_domain'] = '127.0.0.1';
//\$lb['cookie_path']	= '/loubia/';
//\$lb['cookie_secure'] = 0;
?>";

	$lb['db_type'] 	= $_POST['db_type'];
	$lb['db_host'] 	= $_POST['db_host'];
	$lb['db_name'] 	= $_POST['db_name'];
	$lb['db_user'] 	= $_POST['db_user'];
	$lb['db_pass'] 	= $_POST['db_pass'];
	$lb['prefix']	= $_POST['perfix'];
	$lb['cookie']	= $_POST['cookie'];
	$lb['webdir']	= $_POST['webdir'];

	include_once($lb['current_path'] . '/include/db/' . $lb['db_type'] . '.inc.php');
	include_once($lb['current_path'] . '/include/db.inc.php');
	include_once($lb['current_path'] . '/include/system.inc.php');
	include_once($lb['current_path'] . '/include/group.inc.php');
	include_once($lb['current_path'] . '/include/session.inc.php');
	include_once($lb['current_path'] . '/include/user.inc.php');

	$lb['db'] = new lb_db();
	
	$lb['db']->query('CREATE DATABASE #', array(array($lb['db_name'], 'text')));

	$sql_file = file_get_contents($_POST['dir'] . '/sql/' . $_POST['db_type'] . '.sql');
	$tables = preg_split('/;[^\'"\d]/', $sql_file);
			
	foreach($tables as $table){
		$lb['db']->query($table, array());
	}
	
	$lb['session'] = new lb_session();
	$lb['user']	= new lb_user();
	
	lb_group::add_group('Superuser');
	lb_group::add_group('Moderator');
	lb_group::add_group('User');
	
	$user_id = lb_user::add_user($_POST['user_name'], $_POST['user_pass'], $_POST['user_email'], 1);
	
	$lb['session']->set('logged', 1);
	$lb['session']->set('user_id', $user_id);
	
	file_put_contents($_POST['dir'] . '/config/config.inc.php', $config);
	$lb['smarty']->assign ('task', 'install_pages');
	
	$lb['session']->remove('logged');
	$lb['session']->remove('user_id');

}elseif(isset($_GET['install']) && $_GET['install'] == 'pages'){

	include_once($lb['current_path'] . '/config.inc.php');
	include_once($lb['dir'] . 'include/system.inc.php');
	include_once($lb['dir'] . 'include/db/' . $lb['db_type'] . '.inc.php');
	include_once($lb['dir'] . 'include/db.inc.php');
	include_once($lb['dir'] . 'include/session.inc.php');
	include_once($lb['dir'] . 'include/user.inc.php');
	include_once($lb['dir'] . 'include/page.inc.php');
	
	$lb['db']   = new lb_db();
	$lb['session'] = new lb_session();
	
	$lb['session']->set('logged', 1);
	$lb['session']->set('user_id', 1);

	$lb['user'] = new lb_user();
	$lb['page'] = new lb_page(1);
	
	$install_page['title'] = 'Home page';
	$install_page['content'] = 'Edit this page!!';
	
	$pages = lb_page::list_pages();
	foreach($pages as $name=>$page){
		include_once($lb['dir'] . 'page/' . $page['path'] . '/' . $page['class'] . '.inc.php');
		eval('$lb[\'cpage\'] = new ' . $page['class'] . ';');
		$lb['cpage']->install();
		if($name == 'plain'){
			$page_type_id = $lb['cpage']->add_page($install_page);
			$lb['page']->add_page($name, $page_type_id, $install_page['title'], true, 1, 0);
		}
	}
	
	$lb['session']->remove('logged');
	$lb['session']->remove('user_id');
	
	$lb['smarty']->assign ('task', 'install_finish');

}else{

	$lb['smarty']->assign ('task', 'install_system');
}

$lb['smarty']->assign('dir', realpath('../') . '/');
$lb['smarty']->assign('webdir', 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/');
$lb['smarty']->display('install_index.tpl');

?>
